import {
  _decorator,
  Component,
  director,
  instantiate,
  Label,
  Node,
  Prefab,
} from "cc";
import NetworkManager from "../Global/NetworkManager";
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes } from "../Common";
import { PlayerManager } from "../UI/PlayerManager";
import DataManager from "../Global/DataManager";
import { EventEnum, SceneEnum } from "../Enum";
import { RoomManager } from "../UI/RoomManager";
import EventManager from "../Global/EventManager";
const { ccclass, property } = _decorator;

@ccclass("HallManager")
export class HallManager extends Component {
  @property(Node)
  playerContent: Node;

  @property(Prefab)
  playerPref: Prefab;

  @property(Node)
  roomContent: Node;

  @property(Prefab)
  roomPref: Prefab;

  protected onLoad(): void {
    director.preloadScene(SceneEnum.Room);

    EventManager.Instance.on(EventEnum.RoomJoin, this.handleJoinRoom, this);

    NetworkManager.Instance.listenMsg(
      ApiMsgEnum.MsgPlayerList,
      this.renderPlayer,
      this
    );

    NetworkManager.Instance.listenMsg(
      ApiMsgEnum.MsgRoomList,
      this.renderRoom,
      this
    );
  }

  start() {
    this.playerContent.removeAllChildren();
    this.roomContent.removeAllChildren();

    this.getPlayers();
    this.getRooms();
  }

  protected onDestroy(): void {
    NetworkManager.Instance.unlistenMsg(
      ApiMsgEnum.MsgPlayerList,
      this.renderPlayer,
      this
    );

    NetworkManager.Instance.unlistenMsg(
      ApiMsgEnum.MsgRoomList,
      this.renderRoom,
      this
    );

    EventManager.Instance.off(EventEnum.RoomJoin, this.handleJoinRoom, this);
  }

  async getPlayers() {
    const { success, error, res } = await NetworkManager.Instance.callApi(
      ApiMsgEnum.ApiPlayerList,
      {}
    );

    if (!success) {
      console.log(error);
      return;
    }

    this.renderPlayer(res);
  }

  async getRooms() {
    const { success, error, res } = await NetworkManager.Instance.callApi(
      ApiMsgEnum.ApiRoomList,
      {}
    );

    if (!success) {
      console.log(error);
      return;
    }
    console.log("rooms: ", res);
    this.renderRoom(res);
  }

  //   handlePlayerList({ list }: IApiPlayerListRes) {
  //     this.renderPlayer()
  //   }

  async handleJoinRoom(rid: number) {
    let { success, error, res } = await NetworkManager.Instance.callApi(
      ApiMsgEnum.ApiRoomJoin,
      { rid }
    );

    if (!success) {
      console.log(error);
      return;
    }

    console.log("rooms: ", res);
    DataManager.Instance.roomInfo = res.room;
    director.loadScene(SceneEnum.Room);
  }

  async handleCreateRoom() {
    let { success, error, res } = await NetworkManager.Instance.callApi(
      ApiMsgEnum.ApiRoomCreate,
      {}
    );

    if (!success) {
      console.log(error);
      return;
    }

    DataManager.Instance.roomInfo = res.room;
    console.log("Room created", DataManager.Instance.roomInfo);

    director.loadScene(SceneEnum.Room);
  }

  renderPlayer({ list }: IApiPlayerListRes) {
    for (let child of this.playerContent.children) {
      child.active = false;
    }

    while (this.playerContent.children.length < list.length) {
      let newPlayer = instantiate(this.playerPref);
      newPlayer.addComponent(PlayerManager);

      newPlayer.setParent(this.playerContent);
    }

    for (let i = 0; i < list.length; i++) {
      let pm = this.playerContent.children[i].getComponent(PlayerManager);
      pm.init(list[i]);
    }
  }

  renderRoom({ list }: IApiRoomListRes) {
    for (let child of this.roomContent.children) {
      child.active = false;
    }

    while (this.roomContent.children.length < list.length) {
      let newRoom = instantiate(this.roomPref);
      newRoom.setParent(this.roomContent);
    }

    for (let i = 0; i < list.length; i++) {
      let rm = this.roomContent.children[i].getComponent(RoomManager);
      rm.init(list[i]);
    }
  }

  update(deltaTime: number) {}
}
